﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    public class Program
    {
        public unsafe static void Main(string[] arags)
        {
            /*
             *GC调优
             */
            GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;

            //不使用GC，并分配当前程序所需内存。
            //GC.TryStartNoGCRegion(1);

            //启用CG
            //GC.EndNoGCRegion();


            /*
             * 指针赋值
             */
            int tvar = 996;
            int* p = &tvar;
            int** p2 = &p;


            //输出数据
            Console.WriteLine("Data:{0}", tvar);

            //输出指针地址
            Console.WriteLine("Address:{0}", (ulong)p);

            //输出指针指向的数据
            Console.WriteLine("Pointer to data:{0}", *p);
            Console.WriteLine("Pointer to data:{0}", (ulong)*p2);

           


            Console.WriteLine("---------------------------------------------------------------------");

            /*
             * 指针转换
             */
            var vl = 20;
            int* pVl = &vl;
            var a = (ulong)pVl;
            int* b = (int*)a;

            //输出指针地址
            Console.WriteLine("Address:{0}", a);
            Console.WriteLine("Address:{0}", (ulong)pVl);
            Console.WriteLine("Address:{0}", (ulong)b);

            //输出指针指向的数据
            Console.WriteLine("Pointer to data:{0}", *pVl);
            Console.WriteLine("Pointer to data:{0}", *b);

            Console.WriteLine("---------------------------------------------------------------------");

            /*
             * 指针数据交换
             */
            var v1 = 123;
            var v2 = 456;
            Transfrom(&v1, &v2);
            Console.WriteLine("v1:{0},v2:{1}", v1, v2);

            Console.WriteLine("---------------------------------------------------------------------");

            /*
             * 指针运算
             */
            double db = 9.5;
            int it = 11;
            byte by = 127;

            double* pDb = &db;
            int* pIt = &it;
            byte* pBy = &by;


            Console.WriteLine("Double Address:\t\t{0}", (ulong)pDb);
            Console.WriteLine("Int Address:\t\t{0}", (ulong)pIt);
            Console.WriteLine("Byte Address:\t\t{0}\n", (ulong)pBy);

            Console.WriteLine("Add 1 Double Address:\t{0}", (ulong)++pDb);
            Console.WriteLine("Add 1 to Int Address:\t{0}", (ulong)++pIt);
            Console.WriteLine("Add 1 to Byte Address:\t{0}", (ulong)++pBy);

            int* pD1 = (int*)1243332;
            int* pD2 = (int*)1243320;
            Console.WriteLine("pD1 - pD2 = " + (pD1 - pD2).ToString());//result = 3 ((1243332-1243320)/sizeof(int)) sizeof:类型存储单元大小

            Console.WriteLine("---------------------------------------------------------------------");

            /*
            * 结构体指针
            */
            Vector2 vector = new();
            Vector2* pVector = &vector;

            float* pVX = &(vector.x);
            float* pVY = &(pVector->y);

            pVector->x = 1f;//(*pV).x = 1f;
            pVector->y = 2f;//(*pV).y = 2f;

            Console.WriteLine("Vector2 Data:{0} Address:\t{1}", vector.ToString(), (ulong)pVector);
            Console.WriteLine("Vector2.X Data:{0} Address:\t\t{1}", pVector -> x, (ulong)pVY);


            Console.WriteLine("---------------------------------------------------------------------");

            

            /*
             * fixed固定变量 避免GC的堆压缩操作
             */
            var arr = new int[] { 996, 997, 998, 999 };
            fixed (int* arrp = &(arr[0]))
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    Console.WriteLine("address:[{0}],data:[{1}]", (ulong)(arrp + i), (arrp + i)->ToString());
                }
            }


        }

        public unsafe static void Transfrom(int* p1, int* p2) 
        {
            var temp = *p1;
            *p1 = *p2;
            *p2 = temp;

        }

    }
}
